#!/usr/bin/python
#***************************************************************************
# Copyright notice for this file:
#  Copyright (C) 2012 TheAxeMan
#  _______ _                               _    _
# |__   __| |     ___     /\          ___ | \  / |
#    | |  | |__  / _ \   /  \  __  _ / _ \|  \/  | __ _ _ __
#    | |  | '_ \|  __/  / /\ \ \ \/ |  __/| .  . |/ _` | '_ \
#    | |  | | | | (__/\/ ____ \ )  (| (__/| |\/| | (_| | | | |
#    |_|  |_| |_|\____/_/    \_._/\_.\____|_|  |_|\__,_|_| |_|
#
# Final Fantasy TAS scripts by TheAxeMan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#***************************************************************************

import wx
import os
from makefftas import generateRun


class GeneratorFrame(wx.Frame):
  def __init__(self, parent, id=wx.ID_ANY, 
                  title="Final Fantasy TAS Generator"):
    wx.Frame.__init__(self, parent, id, title)
    ib = wx.IconBundle()
    ib.AddIconFromFile("ffmaketasicon.ico",wx.BITMAP_TYPE_ANY)
    self.SetIcons(ib)    
    
    mainPanel = wx.Panel(self)
    
    bgColor = wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE)
    self.SetBackgroundColour(bgColor)

    #Set up a menu bar with just a File menu
    helpmenu= wx.Menu()
    menuAbout = helpmenu.Append(wx.ID_ABOUT, "&About","About this program")

    menuBar = wx.MenuBar()
    menuBar.Append(helpmenu,"&Help")
    self.SetMenuBar(menuBar)

    self.nameCtrls = [wx.TextCtrl(mainPanel, wx.ID_ANY, "", size=(50,-1)) for i in xrange(4)]
    self.classCombo = wx.ComboBox(mainPanel, wx.ID_ANY, style=wx.CB_DROPDOWN|wx.CB_READONLY, choices=["Fighter", "White Mage"])
    self.classCombo.SetValue("Fighter")
    self.generateButton = wx.Button(mainPanel, wx.ID_ANY, "Generate")
    self.exitButton = wx.Button(mainPanel, wx.ID_ANY, "Exit")
    
    buttonSizer = wx.FlexGridSizer(1, 2, 0, 0)
    buttonSizer.Add(self.generateButton, 0, wx.ALL, 10)
    buttonSizer.Add(self.exitButton, 0, wx.ALL, 10)

    self.mainSizer = wx.FlexGridSizer(0, 1, 0, 0)
    for nameCtrl in self.nameCtrls:
      nameCtrl.SetMaxLength(4)
      self.mainSizer.Add(nameCtrl, 0, wx.TOP|wx.ALIGN_CENTER, 10)
    self.mainSizer.Add(self.classCombo, 0, wx.ALL|wx.EXPAND, 10)
    self.mainSizer.Add(wx.StaticLine(mainPanel), 0, wx.LEFT|wx.RIGHT|wx.EXPAND, 10)
    self.mainSizer.Add(buttonSizer, 0, wx.ALIGN_RIGHT, 10)
    
    mainPanel.SetSizer(self.mainSizer)
    actualMainSizer = wx.BoxSizer()
    actualMainSizer.Add(mainPanel, 1, wx.EXPAND)
    self.SetSizer(actualMainSizer)
    self.SetAutoLayout(1)
    actualMainSizer.Fit(self)

    self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
    self.generateButton.Bind(wx.EVT_BUTTON, self.onGenerate)
    self.exitButton.Bind(wx.EVT_BUTTON, self.OnExit)
                             
  def OnAbout(self, event):
    """Display some helpful text on hitting About."""
    dlg = wx.MessageDialog(self, 
"""Final Fantasy TAS Generator by TheAxeMan

Enter the names that will be used in the TAS. Names may include lower- and upper-case letters and numbers and some punctuation. Use : for the '..'

Choose between fighter and white mage runs.

The order of the characters changes from the initial setup but is stable for most of the run. In the TAS, the names entered here will be entered in a different order so that the final order will match this.
    """, "About", wx.OK)
    dlg.ShowModal()
    dlg.Destroy()

  def OnExit(self, event):
    self.Close(True)

  def onGenerate(self, event):
    names = [namectrl.GetValue() for namectrl in self.nameCtrls]
    whitemage = self.classCombo.GetValue() == "White Mage"
    defaultFilename = "FinalFantasy(U)-TheAxeMan-%s(generated)" % (("whitemage",) if whitemage else ("fighter",))
    
    dlg = wx.FileDialog(self, "Location for generated fm2 file", "", defaultFilename, "*.fm2", wx.FD_SAVE)
    if dlg.ShowModal() == wx.ID_OK:
        filename = os.path.join(dlg.GetDirectory(), dlg.GetFilename())
        runData = generateRun(names, whitemage=whitemage, finalorder=True)
        outfile = open(filename, 'w')
        print >> outfile, runData
        outfile.close()
    dlg.Destroy()
    


def main():
  """Main function just creates an app with a DataListFrame."""
  app = wx.App(False)
  frame = GeneratorFrame(None)
  frame.Show(True)
  app.MainLoop()

  
if __name__ == "__main__":
  main()